﻿Module Propis
    Public Function Propis(ByVal Summa As Double) As String
        Dim t As String
        Dim s() As String
        Dim n(0 To 2) As Integer
        Dim i As Integer
        Dim j As Integer
        t = "один два три четыре пять шесть семь восемь девять десять одиннадцать двенадцать тринадцать четырнадцать пятнадцать шестнадцать семнадцать восемнадцать девятнадцать двадцать тридцать сорок пятьдесят шестьдесят семьдесят восемьдесят девяносто сто двести триста четыреста пятьсот шестьсот семьсот восемьсот девятьсот одна две тысяча тысячи тысяч евро копейка копейки копеек ноль рубль рубля рублей"
        s = Split(t, " ")
        If Summa > 999999.99 Or Summa < 0 Then Return 0

        t = Trim(Format(Summa, "# ##0.00"))
        n(0) = CInt(Strings.Right(t, 2))
        t = Strings.Left(t, InStr(1, t, ",") - 1)
        If InStr(1, t, " ") = 0 Then
            n(1) = CInt(t)
            n(2) = 0
        Else
            n(1) = CInt(Mid$(t, InStr(1, t, " ") + 1))
            n(2) = CInt(Strings.Left(t, InStr(1, t, " ") - 1))
        End If
        t = ""
        If n(2) > 0 Then
            i = n(2) \ 100
            If i > 0 Then
                t = s(i + 26)
            End If
            i = n(2) - i * 100
            If i > 0 Then
                Select Case i
                    Case Is = 1
                        t = t & " " & s(36) & " " & s(38)
                    Case Is = 2
                        t = t & " " & s(37) & " " & s(39)
                    Case 3 To 4
                        t = t & " " & s(i - 1) & " " & s(39)
                    Case 5 To 19
                        t = t & " " & s(i - 1) & " " & s(40)
                    Case 20 To 99
                        t = t & " " & s(i \ 10 + 17)
                        i = i Mod 10
                        Select Case i
                            Case Is = 0
                                t = t & " " & s(40)
                            Case Is = 1
                                t = t & " " & s(36) & " " & s(38)
                            Case Is = 2
                                t = t & " " & s(37) & " " & s(39)
                            Case 3 To 4
                                t = t & " " & s(i - 1) & " " & s(39)
                            Case 5 To 9
                                t = t & " " & s(i - 1) & " " & s(40)
                        End Select
                End Select
            Else
                t = t & " " & s(40)
            End If
        End If
        If n(1) > 0 Then
            i = n(1) \ 100
            If i > 0 Then
                t = t & " " & s(i + 26)
            End If
            i = n(1) - i * 100
            If i > 0 Then
                Select Case i
                    Case 1 To 19
                        t = t & " " & s(i - 1)
                    Case 20 To 99
                        t = t & " " & s(i \ 10 + 17)
                        i = i Mod 10
                        If i > 0 Then
                            t = t & " " & s(i - 1)
                        End If
                End Select
            End If
        End If

        Select Case n(1) 'приписывание рублей/рубля/рубль
            Case Is = 0
                t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
            Case Is = 1
                t = t & ")" & " " & s(46) & " " & Format$(n(0), "00")
            Case 2 To 4
                t = t & ")" & " " & s(47) & " " & Format$(n(0), "00")
            Case 5 To 19
                t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
            Case 20 To 999
                i = n(1) Mod 100
                Select Case i
                    Case Is = 0
                        t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                    Case Is = 1
                        t = t & ")" & " " & s(46) & " " & Format$(n(0), "00")
                    Case 2 To 4
                        t = t & ")" & " " & s(47) & " " & Format$(n(0), "00")
                    Case 5 To 9
                        t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                    Case 5 To 19
                        t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                    Case 20 To 99
                        j = n(1) Mod 10
                        Select Case j
                            Case Is = 0
                                t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                            Case Is = 1
                                t = t & ")" & " " & s(46) & " " & Format$(n(0), "00")
                            Case 2 To 4
                                t = t & ")" & " " & s(47) & " " & Format$(n(0), "00")
                            Case 5 To 9
                                t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                            Case 5 To 9
                                t = t & ")" & " " & s(48) & " " & Format$(n(0), "00")
                        End Select
                End Select
                
        End Select


        ' t = t & " " & s(41) & " " & Format$(n(0), "00")
        Select Case n(0)
            Case Is = 0
                t = t & " " & s(44)
            Case Is = 1
                t = t & " " & s(42)
            Case 2 To 4
                t = t & " " & s(43)
            Case 5 To 19
                t = t & " " & s(44)
            Case 20 To 99
                i = n(0) Mod 10
                Select Case i
                    Case Is = 0
                        t = t & " " & s(44)
                    Case Is = 1
                        t = t & " " & s(42)
                    Case 2 To 4
                        t = t & " " & s(43)
                    Case 5 To 9
                        t = t & " " & s(44)
                End Select
        End Select
        t = Trim(t)
        If n(2) = 0 And n(1) = 0 Then t = s(45) & " " & t
        t = UCase(Strings.Left(t, 1)) & Mid(t, 2)
        'Propis = t
        Return t
    End Function
End Module
